<?php
$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BankHelper');
$BankHelper =  new BankHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
//if($member_info['u_dp_type'] == 'dp' && $member_info['u_scope'] == '单位级'){
//    $dp_code = 'GL03';
//}else{
//    $dp_code = $member_info['u_dpcode'];
//}
$dp_code = $member_info['u_dpcode'];
$this->LoadResurces('oldapi/class.carloan');
$oldapi = new Carloan();

$sign = $oldapi->sign(array('code' => $dp_code));
$api_baseurl = OLD_SITE_URL.'/OrderDetails/index/?code='.$dp_code.'&sign='.$sign['sign'];

$this->Assign('member_info', $member_info);

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

if (empty($this->Param['option'])) {
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if ($ajax) {
        $where = array(
            '`o_status` != ?' => empty($this->Param['o_keyword'])?'作废':''
        );
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info);
        $json_data = array('total' => $data['count'], 'rows' => array(), 'where' => $data['where']);
        
        $mode = Config::GetCommon();
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
                $row['o_sell_time'] = empty($row['o_sell_time']) ? '' : date('Y-m-d H:i:s', $row['o_sell_time']);//征信录入时间
                $row['o_search_time'] = empty($row['o_search_time']) ? '' : date('Y-m-d H:i:s', $row['o_search_time']);//征信查询时间
                $row['o_first_time'] = empty($row['o_first_time']) ? '' : date('Y-m-d H:i:s', $row['o_first_time']); //初审提交时间
                $row['o_first_atime'] = empty($row['o_first_atime']) ? '' : date('Y-m-d H:i:s', $row['o_first_atime']); //初审通过时间
                $row['o_finishs_time'] = empty($row['o_finishs_time']) ? '' : date('Y-m-d H:i:s', $row['o_finishs_time']); //终审提交时间
                $row['o_finishe_time'] = $row['o_layer_aucode'] == 'A00001'?  0 : $row['o_finishe_time'];
                $row['o_finishe_time'] = empty($row['o_finishe_time']) ? '' : date('Y-m-d H:i:s', $row['o_finishe_time']);//终审通过
                $order_car = $OrderHelper->orderCar($row['o_id']);
                $row['ot_loan_limit'] = $order_car['ot_loan_limit']; //贷款金额
                //
                if($row['o_oldsys'] == 1){
                    $sign = $oldapi->sign(array('code' => $dp_code));
                    $row['info_url'] = $api_baseurl.'&id='.$row['o_number'];
                }else{
                    $row['info_url'] = '/index/index/option/info?oid='.$row['o_id'];
                }
                $row['o_pre_modetext'] = $mode['mode'][$row['o_pre_mode']]['mode_text'];
                
                $bank_row = $BankHelper->bankRow($row['o_bank_id']);
                $row['o_bank_abb'] = $bank_row['bk_abbreviation'];
                
                $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
    }else{
        echo $this->GetView('index.php');
    }
}else{
    switch ($this->Param['option']) {
        case 'info':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $order = $OrderHelper->orderInfo($oid, true);
            $order['audit_ok'] = $OrderHelper->orderAuditfilter($order['audit_all']['ok'], '初审审核');
            $order['final_ok'] = $OrderHelper->orderAuditfilter($order['audit_all']['ok'], '终审审核');
            $order['review_draft'] = $OrderHelper->orderAuditfilter($order['audit_all']['draft'], '复核审核');
            $order['info_url'] = $api_baseurl.'&type=1&id='.$order['base']['o_number'];
            if($member_info['u_role'] == 4 && $member_info['u_scope'] == '用户级'){ //大区业务员
                $br_code = $MemberHelper->getbranchCode($order['base']['o_branch_id']);
                $sign = $oldapi->sign(array('code' => $br_code));
                $api_baseurl = OLD_SITE_URL.'/OrderDetails/index/?code='.$br_code.'&sign='.$sign['sign'];
                $order['info_url'] = $api_baseurl.'&type=1&id='.$order['base']['o_number'];
            }
            $this->Assign('order', $order);
            echo $this->GetView('index_info.php');
            break;
        case 'querystate':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $o_number = empty($this->Param['o_number']) ? ErrorMsg::Debug('参数错误') : $this->Param['o_number'];
            $carloan = new Carloan();
            switch($member_info['u_scope']){
                case '用户级':
                    $dpcode = '-1';
                    break;
                case '部门级':
                    $dpcode = $member_info['u_dpcode'];
                    break;
                default :
                    $dpcode = '';
            }
            $data = $carloan->getPayorder($dpcode, 1,100000);
            $data = json_decode($data, TRUE);
            foreach($data['root'] as $k => $v){
                if($v['o_number'] == $o_number){
                    ErrorMsg::Debug('该订单请先至<待支付>支付相关费用');
                }
            }
            $result = $OrderHelper ->querystate($oid);
            if($result['success']){
                ErrorMsg::Debug($result['msg']);
            }else{
                ErrorMsg::Debug('获取状态错误');
            }
            break;
    }
}